package com.yunchang.springboot.utils.lightningDeals;

import com.alibaba.fastjson.JSON;
import com.yunchang.springboot.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class KeepaLightningDealsUtil {
    /**
     * 请求基本url
     */
    private static String BASE_URL = "https://api.keepa.com/";

    /**
     * accessKey
     * */
    private static String KEY = "at7ac74g2blftasbcbqih8lvl5fpehq8crd7tupo3jbphvr40v54ovttisoks40d";
//    /**
//     * 请求成功的code
//     */
//    public static String CODE_SUCCESS = "000";
//
//    /**
//     * 请求失败的code
//     */
//    public static String CODE_ERROR = "999";

    /**
     * 获取数据
     *
     * @param params
     * @return
     */
    public static Map<String, Object> getClient(String type, Map<String, String> params) {
        CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom()
                .setConnectionRequestTimeout(180000).setConnectTimeout(180000).setSocketTimeout(180000).build()).build();
        Map<String, String> baseParams = new HashMap<String, String>(7) {{
            put("key", KEY);
        }};
        if (null != params) {
            baseParams.putAll(params);
        }
        StringBuilder stringBuilder = new StringBuilder(BASE_URL + type);
        boolean flag = false;
        for (Map.Entry<String, String> entry : baseParams.entrySet()) {
            String value = entry.getValue();
            String key = entry.getKey();
            if (!flag) {
                stringBuilder.append("?");
                flag = true;
            } else {
                stringBuilder.append("&");
            }
            try {
                stringBuilder.append(key).append("=").append(URLEncoder.encode(value, StandardCharsets.UTF_8.name()));
            } catch (UnsupportedEncodingException e) {
                log.warn("keepa api:url请求参数处理错误");
                return null;
            }
        }
        try {
            HttpGet get = new HttpGet(String.valueOf(stringBuilder));
            CloseableHttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            if (null == entity) {
                log.warn("keepa获取的内容为空");
                return null;
            }
            String result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            EntityUtils.consume(entity);
            int statusCode = response.getStatusLine().getStatusCode();
            if (HttpServletResponse.SC_OK != statusCode) {
                log.warn("keepa api:请求失败，状态码:" + statusCode + "   详情:" + result);
                return null;
            }
            Map<String, Object> resultMap = (Map<String, Object>) JSON.parse(result);
            response.close();
            client.close();
            return resultMap;
        } catch (IOException e) {
            log.warn("keepa api:请求出错,详情:" + e);
            return null;
        }
    }
}
